<template>
  <q-dialog v-model="open" class="task-dialog">
    <q-card class="box" style="margin-top: -550px">
      <q-card-section class="row items-center q-mt-md">
        <q-space />
        <q-btn icon="clear" flat round dense v-close-popup style="color: #fff" />
      </q-card-section>
      <div class="taskInfo">
        <div class="title">
          <span>材料号</span>
          <span>指令类型</span>
          <span>逻辑位置</span>
        </div>
        <div class="content">
          <div
            v-for="ele in rows"
            :key="ele.orderNo"
            @click="handleClick(ele.orderNo)"
            :style="{ backgroundColor: labelColors[ele.orderNo] || '' }"
          >
            <span>{{ ele.matNo }}</span>
            <span>{{ ele.orderType }}</span>
            <span>{{ ele.frPos }}</span>
          </div>
        </div>
      </div>
    </q-card>
  </q-dialog>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { emitter, useEmitter } from 'src/emitter';
import { OrderRow } from './model';
import { getNearByOrder } from '../api';

export default defineComponent({
  name: 'terminal-crane-task',

  setup() {
    const open = ref(false);
    const rows = ref<OrderRow[]>([]);
    const labelColors = ref<Record<string, string>>({}); // 存储标签颜色

    const handleClick = (orderNo: string) => {
      // 重置所有标签颜色为原始颜色
      const newLabelColors: Record<string, string> = {};
      rows.value.forEach(row => {
        newLabelColors[row.orderNo] = ''; // 或者设为原始的颜色，这里假设为空字符串
      });

      // 设置点击的标签颜色为红色，70%透明度
      newLabelColors[orderNo] = 'rgba(255, 0, 0, 0.7)';

      // 更新标签颜色
      labelColors.value = newLabelColors;

      // 触发事件
      emitter.emit('inform', { name: 'terminal-task-detail', value: { orderNo, isOpen: true } });
    };

    useEmitter('inform', async payload => {
      if (payload.name !== 'terminal-task') {
        return;
      }
      const { isOpen, x, y, craneNo, frPos } = payload.value;
      open.value = isOpen;

      if (!isOpen) {
        labelColors.value = {};
      } else {
        const params = { craneNo, x, y };
        const { data } = await getNearByOrder(params);
        rows.value = data;
        // 重置标签颜色
        const newLabelColors: Record<string, string> = {};
        rows.value.forEach(row => {
          newLabelColors[row.orderNo] = '';
        });
        if (frPos) {
          rows.value.forEach(row => {
            if (row.frPos === frPos) {
              newLabelColors[row.orderNo] = 'rgba(255, 0, 0, 0.7)';
            }
          });
        }

        labelColors.value = newLabelColors;
      }
    });

    return {
      open,
      rows,
      labelColors,
      handleClick,
    };
  },
});
</script>

<style lang="scss" scoped>
.box {
  position: relative;
  width: 500px;
  height: 338px;
  background-color: transparent;
  background-image: url('src/assets/terminal/task_bg.png');
  background-size: 100% 100%;
}
.task-dialog {
  position: fixed !important; /* 强制覆盖 Quasar 默认定位 */
  z-index: 1050; /* 设置弹窗层级 */
  /* 默认居中 */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
@media (max-width: 1024px) and (max-height: 768px) {
  .task-dialog {
    top: 50%; /* 设置顶部距离 */
    left: 50%; /* 使弹窗水平居中 */
    transform: translate(-50%, -50%); /* 将弹窗在水平和垂直上移动其自身宽度和高度的一半 */
  }
}
.taskInfo {
  position: absolute;
  width: 336px;
  height: 238px;
  top: 70px;
  left: 0;
  right: 0;
  margin: auto;
  text-align: center;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  .title {
    display: flex;
    width: 100%;
    height: 40px;
    justify-content: space-between;
    line-height: 40px;
    span {
      width: 105px;
      height: 40px;
      background-image: url('src/assets/terminal/task_title_bg.png');
      background-size: 100% 100%;
    }
  }
  .content {
    width: 100%;
    height: 198px;
    overflow: overlay !important;
    div {
      display: flex;
      width: 100%;
      height: 30px;
      line-height: 30px;
      justify-content: space-between;
      cursor: pointer;
      &:hover {
        background-color: rgba(0, 0, 0, 0.3);
      }
      span {
        width: 105px;
        height: 100%;
        background-size: 100% 100%;
      }
    }
  }
}

/*自定义滚动条样式*/
.taskInfo ::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 5px;
}

.taskInfo ::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 5px;
  background: rgb(153, 153, 153, 0.5);
}

.taskInfo ::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  border-radius: 5px;
  background: rgb(128, 128, 128);
}
</style>
